package com.zhoug.mpchart.markers;

import android.graphics.Bitmap;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.SparseArray;
import android.view.View;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

/**
 *RecyclerView.ViewHolder 基类
 */
@SuppressWarnings("unchecked")
 public class MPBaseViewHolder extends RecyclerView.ViewHolder {
    private Object tag;
    /**
     * 存储布局中的字组件item.findViewById集合
     */
    private SparseArray<View> mViews;


    /**
     * 关联的BaseRecyclerViewAdapter
     */
    private MPBaseRecyclerViewAdapter<?> adapter;


    public MPBaseViewHolder(View itemView) {
        super(itemView);
        mViews=new SparseArray<>();

    }

    public MPBaseViewHolder(View itemView, MPBaseRecyclerViewAdapter<?> adapter) {
        super(itemView);
        mViews=new SparseArray<>();
        this.adapter=adapter;
    }



    /**
     * 根据id获取布局文件中的子组件
     * @param id
     * @param <T>
     * @return
     */
    public <T extends View> T getView(@IdRes int id){
        View view=mViews.get(id);
        if(view==null){
            view=itemView.findViewById(id);
            mViews.put(id,view);
        }
        return (T)view;
    }

    /**
     * 给TextView即TextView的子类设置文字
     * @param viewId 组件id
     * @param value
     */
    public  <T extends TextView> T setText(@IdRes int viewId, CharSequence value){
        TextView textView=getView(viewId);
        textView.setText(value);
        return (T) textView;
    }


    /**
     * 设置图片
     * @param viewId
     * @param drawable
     * @return
     */
    public <T extends ImageView> T  setImage(@IdRes int viewId, Drawable drawable) {
        ImageView view = getView(viewId);
        view.setImageDrawable(drawable);
        return (T) view;
    }

    /**
     * 设置图片
     * @param viewId
     * @param bitmap
     * @return
     */
    public <T extends ImageView> T setImage(@IdRes int viewId, Bitmap bitmap) {
        ImageView view = getView(viewId);
        view.setImageBitmap(bitmap);
        return (T) view;
    }

    /**
     * 设置图片
     * @param viewId
     * @param imageResId
     * @return
     */
    public <T extends ImageView> T setImage(@IdRes int viewId, @DrawableRes int imageResId) {
        ImageView view = getView(viewId);
        view.setImageResource(imageResId);
        return (T) view;
    }

    /**
     * 设置选择状态
     * @param viewId
     * @param checked
     * @return
     */
    public <T extends Checkable> T setChecked(@IdRes int viewId, boolean checked) {
        View view = getView(viewId);
        if (view instanceof Checkable) {
            ((Checkable) view).setChecked(checked);
            return (T) view;
        }
        return null;
    }

    /**
     *设置此视图的启用状态
     * @param viewId
     * @param enable
     * @return
     */
    public <T extends View> T setEnabled(@IdRes int viewId, boolean enable) {
        View view = getView(viewId);
        view.setEnabled(enable);
        return (T) view;
    }
    /**
     *设置此视图的选中状态
     * @param viewId
     * @param enable
     * @return
     */
    public <T extends View> T setSelected(@IdRes int viewId, boolean enable) {
        View view = getView(viewId);
        view.setSelected(enable);
        return (T) view;
    }


    /**
     *设置tag
     * @param viewId
     * @param key 必须是资源文件中定义的id
     * @param tag
     * @return
     */
    public <T extends View> T setTag(@IdRes int viewId, @IdRes int key, Object tag) {
        View view = getView(viewId);
        view.setTag(key, tag);
        return (T) view;
    }

    /**
     * 设置tag
     * @param viewId
     * @param tag
     * @return
     */
    public <T extends View> T setTag(@IdRes int viewId, Object tag) {
        View view = getView(viewId);
        view.setTag(tag);
        return (T) view;
    }

    /**
     * 设置item背景
     * @param color
     * @return
     */
    public MPBaseViewHolder setBackgroundColor(@ColorInt int color) {
        itemView.setBackgroundColor(color);
        return this;
    }

    /**
     * 设置item背景
     * @param resId
     * @return
     */
    public MPBaseViewHolder setBackgroundResource(@DrawableRes int resId) {
        itemView.setBackgroundResource(resId);
        return this;
    }

    /**
     * 设置item背景
     * @param background
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public MPBaseViewHolder setBackground(Drawable background) {
        itemView.setBackground(background);
        return this;
    }

    /**
     * 设置指定view的背景
     * @param viewId
     * @param color
     * @return
     */
    public  <T extends View> T setBackgroundColor(@IdRes int viewId, @ColorInt int color) {
        View view = getView(viewId);
        view.setBackgroundColor(color);
        return (T) view;
    }

    /**
     * 设置指定view的背景
     * @param viewId
     * @param resId
     * @return
     */
    public  <T extends View> T setBackgroundResource(@IdRes int viewId, @DrawableRes int resId) {
        View view = getView(viewId);
        view.setBackgroundResource(resId);
        return (T) view;
    }

    /**
     * 设置指定view的背景
     * @param viewId
     * @param background
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public <T extends View> T setBackground(@IdRes int viewId, Drawable background) {
        View view = getView(viewId);
        view.setBackground(background);
        return (T) view;
    }

    /**
     * 设置文字颜色
     * @param viewId
     * @param color
     * @return
     */
    public <T extends TextView> T setTextColor(@IdRes int viewId, @ColorInt int color){
        TextView textView = getView(viewId);
        textView.setTextColor(color);
        return (T) textView;
    }

    /**
     * 设置 setVisibility
     * @param viewId
     * @param visible One of {@link View#VISIBLE}, {@link  View#INVISIBLE}, or {@link  View#GONE}.
     * @return
     */
    public <T extends View> T setVisibility(@IdRes int viewId, int visible) {
        View view = getView(viewId);
        view.setVisibility(visible);
        return (T) view;
    }

    /**
     * 设置字体
     * @param viewId
     * @param typeface
     * @return
     */
    public <T extends TextView> T setTypeface(@IdRes int viewId, Typeface typeface) {
        TextView view = getView(viewId);
        view.setTypeface(typeface);
        view.setPaintFlags(view.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);
        return (T) view;
    }



    public MPBaseRecyclerViewAdapter<?> getAdapter() {
        return adapter;
    }

    public void setAdapter(MPBaseRecyclerViewAdapter<?> adapter) {
        this.adapter = adapter;
    }

    public Object getTag() {
        return tag;
    }

    public void setTag(Object tag) {
        this.tag = tag;
    }

}
